<template>
  <div class="app-container">
    <el-tree
      size="mini"
      :data="typeList"
      node-key="id"
      @node-click="nodeClick"
      v-loading="loading"
      default-expand-all
      :expand-on-click-node="false"
      :render-content="renderContent"
    >
    </el-tree>
  </div>
</template>

<script>
import {
  listType,
  getType,
  delType,
  addType,
  updateType,
} from "@/api/maintype/type";
import { id } from "@/api/system/UUID";
export default {
  name: "MainType",
  props: {
    typeCode: {
      type: [String, Number],
      default: "",
    },
    isTree: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      id: 1,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      // 主分类表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        name: null,
        typeCode: null,
        parentId: null,
        idPath: null,
        orderIndex: null,
        fullIndex: null,
        opId: null,
        opName: null,
        deptId: null,
        deptName: null,
        opTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        opTime: [
          { required: true, message: "操作时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    append(data) {
      this.$prompt("请输入分类名称", "", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputValidator: value => {
          if (
            value == "" ||
            value == undefined ||
            value == null ||
            value == "null"
          ) {
            this.message = "主分类名称不能为空";
            return "主分类名称不能为空";
          }
          if (value.length > 6) {
            this.message = "主分类名称长度不能超过6位";
            return "主分类名称长度不能超过6个字符";
          }
          return true;
        },
        // inputErrorMessage: this.message
      })
        .then(({ value }) => {
          id().then(response => {
            this.form.name = value;
            this.form.typeCode = this.typeCode;
            this.form.idPath = data.idPath + "*" + response.msg;
            this.form.parentId = data.id;
            this.form.id = response.msg;
            addType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.getList();
              const newChild = { id: this.id++, label: value, children: [] };
              if (!data.children) {
                this.$set(data, "children", []);
              }
              data.children.push(newChild);
            });
          });
        })
        .catch(() => {});
    },
    nodeClick(data) {
      if (data.id == "650c44e1-b575-4b62-9d54-1ba9d1e8abb7") {
        data.id = "650c44e1-b575-4b62-9d54-1ba9d1e8abb7";
      }
      this.$emit("getNode", data);
    },
    remove(node, data) {
      const parent = node.parent;
      const children = parent.data.children || parent.data;
      const index = children.findIndex(d => d.id === data.id);
      children.splice(index, 1);
    },

    /** 查询主分类列表 */
    getList() {
      this.queryParams.typeCode = this.typeCode;
      this.loading = true;
      listType(this.queryParams).then(response => {
        this.typeList = response.data;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        typeCode: null,
        parentId: null,
        idPath: null,
        orderIndex: null,
        fullIndex: null,
        opId: null,
        opName: null,
        deptId: null,
        deptName: null,
        opTime: null,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加主分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getType(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改主分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(node, row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          return delType(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
          const parent = node.parent;
          const children = parent.data.children || parent.data;
          const index = children.findIndex(d => d.id === data.id);
          children.splice(index, 1);
        })
        .catch(() => {});
    },
    renderContent(h, { node, data, store }) {
      var path = [];
      if (data.idPath.indexOf("*") != -1) {
        path = data.idPath.split("*");
      }
      if (data.parentId == "00000000-0000-0000-0000-000000000000") {
        if (this.isTree) {
          return (
            <span class="custom-tree-node">
              <span>{node.label}</span>
              <span v-if="isTree">
                <el-button
                  size="mini"
                  icon="el-icon-plus"
                  type="text"
                  on-click={() => this.append(data)}
                ></el-button>
              </span>
            </span>
          );
        } else {
          return (
            <span class="custom-tree-node">
              <span>{node.label}</span>
            </span>
          );
        }
      } else if (path.length > 3) {
        if (this.isTree) {
          return (
            <span class="custom-tree-node">
              <span>{node.label}</span>
              <span v-if="isTree">
                <el-button
                  size="mini"
                  icon="el-icon-delete"
                  type="text"
                  on-click={() => this.handleDelete(node, data)}
                ></el-button>
              </span>
            </span>
          );
        } else {
          return (
            <span class="custom-tree-node">
              <span>{node.label}</span>
            </span>
          );
        }
      } else {
        if (this.isTree) {
          return (
            <span class="custom-tree-node">
              <span>{node.label}</span>
              <span v-if="isTree">
                <el-button
                  size="mini"
                  icon="el-icon-plus"
                  type="text"
                  on-click={() => this.append(data)}
                ></el-button>
                <el-button
                  size="mini"
                  icon="el-icon-delete"
                  type="text"
                  on-click={() => this.handleDelete(node, data)}
                ></el-button>
              </span>
            </span>
          );
        } else {
          return (
            <span class="custom-tree-node">
              <span>{node.label}</span>
            </span>
          );
        }
      }
    },
  },
};
</script>
<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  padding-right: 8px;
    color: black;
  padding-left: 16px;
  background: url("../../images/01.png") no-repeat 0 7px;
  background-size: 13px 13px;
}

</style>
